/** 
	@file ldapauth_plus.cpp
	@brief Config panel for password strength settings
*/
/*
pGina ldapAuth code - based upon skeleton code for pGina plugin development
Copyright (C) 2002 Micah Cooper

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

Email: coopermj@muohio.edu
Snail Mail:
	Micah Cooper
	212A Kreger Hall
	Miami University
	Oxford OH  45056
*/
// ConfigPwd.cpp : implementation file
//

#include "stdafx.h"
#include "winLDAP.h"
#include "ConfigPwd.h"
#include ".\configpwd.h"


// CConfigPwd dialog

IMPLEMENT_DYNAMIC(CConfigPwd, CDialog)
CConfigPwd::CConfigPwd(CWnd* pParent /*=NULL*/)
	: CDialog(CConfigPwd::IDD, pParent)
	, m_requireLower(TRUE)
	, m_requireUpper(TRUE)
	, m_requireNumeric(TRUE)
	, m_requireSpecial(TRUE)
	, m_pwdMinLength(_T("8"))
	, m_disablePwd(FALSE)
	, m_restrictChars(_T(""))
{
	CString temp;
	DWORD dwdTmp;

	dwdTmp = regReadDword(L"requireLower");
	if(dwdTmp == 1)
		m_requireLower = true;
	else
		m_requireLower = false;

	dwdTmp = regReadDword(L"requireUpper");
	if(dwdTmp == 1)
		m_requireUpper = true;
	else
		m_requireUpper = false;

	dwdTmp = regReadDword(L"requireNumeric");
	if(dwdTmp == 1)
		m_requireNumeric = true;
	else
		m_requireNumeric = false;

	dwdTmp = regReadDword(L"requireSpecial");
	if(dwdTmp == 1)
		m_requireSpecial = true;
	else
		m_requireSpecial = false;

	m_pwdMinLength.Format(L"%d", regReadDword(L"pwdMinLength"));
	m_restrictChars = regReadString(L"restrictedChars");

	dwdTmp = regReadDword(L"disablePwd");
	if(dwdTmp == 1) {
		m_disablePwd = true;
	}
	else
		m_disablePwd = false;
}

CConfigPwd::~CConfigPwd()
{
}

void CConfigPwd::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Check(pDX, IDC_LOWERCASE, m_requireLower);
	DDX_Check(pDX, IDC_UPPERCASE, m_requireUpper);
	DDX_Check(pDX, IDC_NUMERIC, m_requireNumeric);
	DDX_Check(pDX, IDC_SPECIAL, m_requireSpecial);
	DDX_Text(pDX, IDC_PWD_LEN, m_pwdMinLength);
	DDX_Check(pDX, IDC_DISABLE_PWD, m_disablePwd);
	DDX_Text(pDX, IDC_RESTRICT_CHARS, m_restrictChars);

	DDX_Control(pDX, IDC_DISABLE_PWD, c_disablePwd);
	DDX_Control(pDX, IDC_MIN_STATIC, c_minLengthText);
	DDX_Control(pDX, IDC_PWD_LEN, c_pwdLenValue);
	DDX_Control(pDX, IDC_SPIN1, c_minSpin);
	DDX_Control(pDX, IDC_REST_STATIC, c_restrictText);
	DDX_Control(pDX, IDC_RESTRICT_CHARS, c_restrictChars);
	DDX_Control(pDX, IDC_REQ_STATIC, c_requiredText);
	DDX_Control(pDX, IDC_LOWERCASE, c_lowerCase);
	DDX_Control(pDX, IDC_UPPERCASE, c_upperCase);
	DDX_Control(pDX, IDC_NUMERIC, c_numeric);
	DDX_Control(pDX, IDC_SPECIAL, c_special);
}


BEGIN_MESSAGE_MAP(CConfigPwd, CDialog)
	ON_BN_CLICKED(IDC_DISABLE_PWD, OnDisableChangePwd)
END_MESSAGE_MAP()


// CConfigPwd message handlers
void CConfigPwd::OnOK()
{
	return;
}

BOOL CConfigPwd::OnInitDialog() 
{
  CDialog::OnInitDialog();
  // Check if change password enabled
  OnDisableChangePwd();

  // Set buddy and range for spin button
  CSpinButtonCtrl* pSpinRange = (CSpinButtonCtrl*)GetDlgItem(IDC_SPIN1);
  pSpinRange->SetRange(0,15);
  pSpinRange->SetBuddy(GetDlgItem(IDC_PWD_LEN));

  return TRUE;  // Return TRUE unless you set the focus to a control.
                // EXCEPTION: OCX Property Pages should return FALSE
}

void CConfigPwd::OnDisableChangePwd()
{
	// TODO: Add your control notification handler code here
	// If checked, need to gray out other options
	if (c_disablePwd.GetCheck() == BST_CHECKED) {
		c_minLengthText.EnableWindow(false);
		c_pwdLenValue.EnableWindow(false);
		c_minSpin.EnableWindow(false);
		c_restrictChars.EnableWindow(false);
		c_restrictText.EnableWindow(false);
		c_requiredText.EnableWindow(false);
		c_lowerCase.EnableWindow(false);
		c_upperCase.EnableWindow(false);
		c_numeric.EnableWindow(false);
		c_special.EnableWindow(false);
	}
	else {
		c_minLengthText.EnableWindow(true);
		c_pwdLenValue.EnableWindow(true);
		c_minSpin.EnableWindow(true);
		c_restrictChars.EnableWindow(true);
		c_restrictText.EnableWindow(true);
		c_requiredText.EnableWindow(true);
		c_lowerCase.EnableWindow(true);
		c_upperCase.EnableWindow(true);
		c_numeric.EnableWindow(true);
		c_special.EnableWindow(true);
	}
}

void CConfigPwd::OnSave()
{
	UpdateData(TRUE);

	if(m_requireLower)
		regWriteDword(1,L"requireLower");
	else
		regWriteDword(0,L"requireLower");

	if(m_requireUpper)
		regWriteDword(1,L"requireUpper");
	else
		regWriteDword(0,L"requireUpper");

	if(m_requireNumeric)
		regWriteDword(1,L"requireNumeric");
	else
		regWriteDword(0,L"requireNumeric");

	if(m_requireSpecial)
		regWriteDword(1,L"requireSpecial");
	else
		regWriteDword(0,L"requireSpecial");

	if(m_pwdMinLength.GetLength() > 0)
		regWriteDword(_wtoi(m_pwdMinLength.GetBuffer(m_pwdMinLength.GetLength())),L"pwdMinLength");

	if(m_restrictChars.GetLength() > 0)
		regWriteString(m_restrictChars.GetBuffer(m_restrictChars.GetLength()),L"restrictedChars");
	else
		regDelValue(L"restrictedChars");

	if(m_disablePwd)
		regWriteDword(1,L"disablePwd");
	else
		regWriteDword(0,L"disablePwd");
}
